﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DGSFramework;
using System.Xml.Linq;
using System.Web.UI.HtmlControls;

namespace Framework
{
    public partial class ArticleDetails : System.Web.UI.Page
    {
        public DGSFrameworkEntities entity = new DGSFrameworkEntities();

        public List<udv_Categories> allCate = new List<udv_Categories>();
        public List<udv_Articles> allArt = new List<udv_Articles>();
        public List<udv_Products> allPro = new List<udv_Products>();

        public udv_Articles articleDetails = new udv_Articles();
        public List<udv_Products> lstProductRelate = new List<udv_Products>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadCacheCategory();
                LoadCacheArticle();
                LoadArticleDetails();
            }
        }

        public void LoadCacheCategory()
        {
            if (Cache["Category"] == null)
            {
                Cache["Category"] = allCate = entity.udv_Categories.ToList();
            }
            else
            {
                allCate = (List<udv_Categories>)Cache["Category"];
            }
        }

        public void LoadCacheArticle()
        {
            if (Cache["Article"] == null)
            {
                Cache["Article"] = allArt = entity.udv_Articles.Include("udv_Categories").ToList();
            }
            else
            {
                allArt = (List<udv_Articles>)Cache["Article"];
            }
        }

        public void LoopCategoryChild(ref List<udv_Categories> allCateChild, int cateID)
        {
            udv_Categories cate = allCate.Where(ca => ca.CateID == cateID).FirstOrDefault();
            if (cate != null)
            {
                allCateChild.Insert(0, cate);
                LoopCategoryChild(ref allCateChild, cate.ParentID.Value);
            }
        }

        public void LoadBreadcrumb(int cateID, ref Repeater rpBreadcrumb)
        {
            List<udv_Categories> allCateChild = new List<udv_Categories>();
            LoopCategoryChild(ref allCateChild, cateID);
            allCateChild.RemoveAt(0);

            rpBreadcrumb.DataSource = allCateChild;
            rpBreadcrumb.DataBind();
        }

        //public void LoadCateLevel(udv_Categories cateCurrent)
        //{
        //    List<udv_Categories> lstCateLevel = allCate
        //        .Where(ca => ca.IsActive == true && ca.ParentID == cateCurrent.CateID).ToList();
        //    ltrParentCate.Text = Globals.GetLanguage(cateCurrent.Title, cateCurrent.Title_en, true);
        //    if (lstCateLevel.Count == 0)
        //    {
        //        lstCateLevel = allCate
        //        .Where(ca => ca.IsActive == true && ca.ParentID == cateCurrent.ParentID).ToList();
        //        udv_Categories cateParent = allCate.Where(ca => ca.CateID == cateCurrent.ParentID).FirstOrDefault();
        //        if (cateParent.CateID != ConfigPaths.Category_Article_ID && cateParent.CateID != ConfigPaths.Category_Product_ID)
        //        {
        //            ltrParentCate.Text = Globals.GetLanguage(cateParent.Title, cateParent.Title_en, true);
        //        }
        //    }
        //    rpCateLevel.DataSource = lstCateLevel;
        //    rpCateLevel.DataBind();
        //}
        
        //public List<udv_Articles> GetArticleRelate(string position, udv_Articles article)
        //{
        //    List<udv_Articles> lstArticleRelate = new List<udv_Articles>();
        //    article.udv_Relate.Load();
        //    var lstRelate = article.udv_Relate.Where(tg => tg.Position.ToLower().Trim() == position.ToLower().Trim()).ToList();
        //    lstRelate.ForEach(tg => lstArticleRelate.Add(allArt.Where(ar => ar.ArticleID == tg.PostID).FirstOrDefault()));
        //    return lstArticleRelate;
        //}

        //    public void LoadArticleRelate(int currentCateArtID)
        //{
        //    try
        //    {
        //        int currentArtID = Int32.Parse(Request.QueryString["ArticleID"]);
        //        allArt = entity.udv_Articles.Where(a => a.ArticleID != currentArtID && (a.udv_Categories.CateID == currentCateArtID)).ToList();
        //        if (allArt.Count == 0)
        //        {
        //            divRelateArt.Visible = false;
        //        }
        //        rptRelateArticle.DataSource = allArt;
        //        rptRelateArticle.DataBind();
        //    }
        //    catch (Exception)
        //    {

        //        throw;
        //    }
        //}

        public void LoadArticleDetails()
        {
            if (Request.QueryString["ArticleID"] != null)
            {
                int articleID;
                int.TryParse(Request.QueryString["ArticleID"].ToString(), out articleID);
                articleDetails = entity.udv_Articles.Where(ar => ar.ArticleID == articleID && ar.IsApproved == true).FirstOrDefault();
                if (articleDetails != null)
                {
                    string strUrlOriginal = ConfigPaths.Domain_Url + Globals.GetUrl(articleDetails.UrlRewrite, Globals.GetUrlAspx(articleDetails.UrlRewrite, false) + articleDetails.ArticleID);
                    if (CheckUrlOriginal(strUrlOriginal) == false)
                    {
                        Response.Redirect(strUrlOriginal);
                    }

                    articleDetails.ViewCount += 1;
                    entity.SaveChanges();

                    articleDetails.udv_CategoriesReference.Load();
                    //LoadBreadcrumb(articleDetails.LoadCateLevel(articleDetails.udv_Categudv_Categories.CateID, ref rpBreadcrumb),
                    //Lories);
                    //ltrParentCate.Text = Globals.GetLanguage(articleDetails.udv_Categories.Title, articleDetails.udv_Categories.Title_en, true);
                    ltrArticleTitle.Text = Globals.GetLanguage(articleDetails.Title, articleDetails.Title_en, true);
                    imgartdetail.ImageUrl = ConfigPaths.Path_ImageArticle_Folder.Substring(1) + articleDetails.ImageUrl;
                    imgartdetail.Width = 698;
                    imgartdetail.Height = 120;
                    //lblArtDate.Text = articleDetails.AddedDate.ToString();
                    ltrArticleContent.Text = Globals.GetLanguage(articleDetails.Body, articleDetails.Body_en, true);
                    //LoadArticleRelate(articleDetails.udv_Categories.CateID);
                    
                }
                else
                {
                    Response.Redirect(ResolveUrl("~/" + Globals.GetUrl("Errors", "Errors.aspx")));
                }
            }
            else
            {
                Response.Redirect(ResolveUrl("~/" + Globals.GetUrl("Errors", "Errors.aspx")));
            }
        }

        public bool CheckUrlOriginal(string urlOriginal)
        {
            string strUrlRequest = ConfigPaths.Domain_Url.Substring(0, ConfigPaths.Domain_Url.Length - 1) + (Request.ServerVariables["HTTP_X_REWRITE_URL"] != null ? Request.ServerVariables["HTTP_X_REWRITE_URL"].ToString() : Request.RawUrl);
            if (strUrlRequest.ToLower().Trim() == urlOriginal.ToLower().Trim())
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}